using System;
using UnityEngine;
using UnityEngine.Serialization;

namespace Life.ESound.Bridge
{
    public class EffectVolumeSwitch : MonoBehaviour
    {
        [FormerlySerializedAs("bgmOffFlag")] [SerializeField] private GameObject volumeOffFlag;
        private bool _isCheckState = false;
        private float _volume = 0;
        private void Start()
        {
            Initialize();
        }

        void Initialize()
        {
            if (ESoundBridge.IsInitialized)
            {
                CheckState();
            }
            else
            {
                ESoundBridge.OnInitialized += CheckState;
            }
        }

        void CheckState()
        {
            _isCheckState = true;
            _volume = ESoundBridge.EffectVolume;
            volumeOffFlag.SetActive(ESoundBridge.EffectVolume == 0);
        }
        
        public void OnSwitch()
        {
            if (_isCheckState)
            {
                if (ESoundBridge.EffectVolume == 0)
                {
                    ESoundBridge.EffectVolume = _volume;
                    volumeOffFlag.SetActive(false);
                }
                else
                {
                    ESoundBridge.EffectVolume = 0;
                    volumeOffFlag.SetActive(true);
                }
            }
        }
    }
}
